package com.dstz.bpm.engine.action.helper;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.bpm.api.engine.action.cmd.InstanceActionCmd;
import com.dstz.org.api.model.IUser;
import com.dstz.sys.util.ContextUtil;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Component
@Scope("prototype")
public class StartNewFlow implements Runnable {
   private static Logger logger = LoggerFactory.getLogger(StartNewFlow.class);
   private static String aO = "startingNewFlow";
   private CountDownLatch aP;
   private Exception exception;
   InstanceActionCmd aQ;
   IUser aR;
   private Map<Object, Object> aS;

   public void run() {
      this.f();
      if (this.aQ != null && this.aR != null) {
         try {
            ContextUtil.setCurrentUser(this.aR);
            this.aQ.executeCmd();
            logger.debug("新流程启动成功！ ");
            this.aP.countDown();
         } catch (Exception var2) {
            this.exception = var2;
            this.aP.countDown();
            logger.error("启动新流程流程失败！" + var2.getMessage());
            var2.printStackTrace();
         }

      } else {
         throw new BusinessException("启动新流程失败！ new flow Cmd or starUser cannot be null");
      }
   }

   public static boolean e() {
      return false;
   }

   public CountDownLatch getLatch() {
      return this.aP;
   }

   public void setLatch(CountDownLatch latch) {
      this.aP = latch;
   }

   public Exception getException() {
      return this.exception;
   }

   public void setException(Exception exception) {
      this.exception = exception;
   }

   public void setUser(IUser user) {
      this.aR = user;
   }

   public InstanceActionCmd getInstanceCmd() {
      return this.aQ;
   }

   public void setInstanceCmd(InstanceActionCmd instanceCmd) {
      this.aQ = instanceCmd;
   }

   private void f() {
      if (!MapUtils.isEmpty(this.aS)) {
         Iterator var1 = this.aS.entrySet().iterator();

         while(var1.hasNext()) {
            Entry<Object, Object> entry = (Entry)var1.next();
            TransactionSynchronizationManager.bindResource(entry.getKey(), entry.getValue());
         }

      }
   }

   public void setTransactionResource(Map<Object, Object> transactionResource) {
      this.aS = transactionResource;
   }
}
